{% extends 'form_div_layout.html.twig' %}

{# On commence par simplement ajouter le form-group au row de nos formulaires #}
{% block form_row %}
    {% spaceless %}
        <div class="form-group {{ errors|length > 0 ? 'has-error' : '' }}">
            {{ form_label(form) }}
            {{ form_widget(form) }}
            {{ form_errors(form) }}
        </div>
    {% endspaceless %}
{% endblock form_row %}

{# Puis on modifie très simplement nos input et textarea
les plus importants pour y ajouter le class imposée par Bootstrap 3 #}
{% block textarea_widget %}
    {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
    {{ parent() }}
{% endblock textarea_widget %}

{% block form_widget_simple %}
    {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
    {{ parent() }}
{% endblock form_widget_simple %}

{% block form_label %}
    {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' control-label')|trim}) %}
    {{ parent() }}
{% endblock form_label %}

{# et enfin les erreurs #}
{% block form_errors %}
    {% spaceless %}
        {% if errors|length > 0 %}
            <span class="help-block alert-danger">
{% for error in errors %}
    {{ error.message }}<br />
{% endfor %}
</span>
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}

{#{% block form_widget %}#}
    {#{% spaceless %}#}
        {#{{ block('field_rows') }}#}
        {#{{ form_rest(form) }}#}
    {#{% endspaceless %}#}
{#{% endblock form_widget %}#}

{#{% block collection_widget %}#}
    {#{% spaceless %}#}
        {#{% if prototype is defined %}#}
            {#{% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}#}
        {#{% endif %}#}
        {#{{ block('form_widget') }}#}
    {#{% endspaceless %}#}
{#{% endblock collection_widget %}#}

{#{% block textarea_widget %}#}
    {#{% spaceless %}#}
        {#<div class="controls">#}
            {#<textarea {{ block('widget_attributes') }}>{{ value }}</textarea>#}
        {#</div>#}
    {#{% endspaceless %}#}
{#{% endblock textarea_widget %}#}

{% block choice_widget %}
    {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
    {% spaceless %}
        {% if expanded %}
            <div class="controls" {{ block('widget_container_attributes') }}>
                {% for child in form %}
                    <label class="radio">
                        {{ form_widget(child) }}
                        {{ form_label(child, null, {'radio': true}) }}
                    </label>
                {% endfor %}
            </div>
        {% else %}
            <div class="controls">
                <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple" class="multiselect" {% endif %}>
                    {% if empty_value is not none %}
                        <option value="">{{ empty_value|trans }}</option>
                    {% endif %}
                    {% if preferred_choices|length > 0 %}
                        {% set options = preferred_choices %}
                        {{ block('widget_choice_options') }}
                        {% if choices|length > 0 and separator is not none %}
                            <option disabled="disabled">{{ separator }}</option>
                        {% endif %}
                    {% endif %}
                    {% set options = choices %}
                    {{ block('widget_choice_options') }}
                </select>
            </div>
        {% endif %}
    {% endspaceless %}
{% endblock choice_widget %}

{% block widget_choice_options %}
    {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
    {% spaceless %}
        {% for group_label, choice in options %}
            {% if choice is iterable %}
                <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                    {% set options = choice %}
                    {{ block('widget_choice_options') }}
                </optgroup>
            {% else %}
                <option {% if choice.disabled is defined and choice is not selectedchoice(value) %}disabled="{{choice.disabled}}"{% endif %} value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
            {% endif %}
        {% endfor %}
    {% endspaceless %}
{% endblock widget_choice_options %}

{% block checkbox_widget %}
    {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
    {% set help = attr.help|default('') %}
    {% spaceless %}
        <div class="controls">
            <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %}
                value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
            {{ help }}
        </div>
    {% endspaceless %}
{% endblock checkbox_widget %}

{#{% block radio_widget %}#}
    {#{% spaceless %}#}
        {#<input type="radio" {{ block('widget_attributes') }}{% if value is defined %}#}
            {#value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />#}
    {#{% endspaceless %}#}
{#{% endblock radio_widget %}#}

{#{% block datetime_widget %}#}
    {#{% spaceless %}#}
        {#{% if widget == 'single_text' %}#}
            {#{{ block('field_widget') }}#}
        {#{% else %}#}
            {#<div {{ block('widget_container_attributes') }}>#}
                {#{{ form_errors(form.date) }}#}
                {#{{ form_errors(form.time) }}#}
                {#{{ form_widget(form.date) }}#}
                {#{{ form_widget(form.time) }}#}
            {#</div>#}
        {#{% endif %}#}
    {#{% endspaceless %}#}
{#{% endblock datetime_widget %}#}

{#{% block date_widget %}#}
    {#{% spaceless %}#}
        {#{% if widget == 'single_text' %}#}
            {#{{ block('field_widget') }}#}
        {#{% else %}#}
            {#<div {{ block('widget_container_attributes') }}>#}
                {#{{ date_pattern|replace({#}
                {#'{{ year }}': form_widget(form.year),#}
                {#'{{ month }}': form_widget(form.month),#}
                {#'{{ day }}': form_widget(form.day),#}
                {#})|raw }}#}
            {#</div>#}
        {#{% endif %}#}
    {#{% endspaceless %}#}
{#{% endblock date_widget %}#}

{#{% block time_widget %}#}
    {#{% spaceless %}#}
        {#{% if widget == 'single_text' %}#}
            {#{{ block('field_widget') }}#}
        {#{% else %}#}
            {#<div {{ block('widget_container_attributes') }}>#}
                {#{{ form_widget(form.hour, { 'attr': { 'size': '1' } }) }}:{{ form_widget(form.minute, { 'attr': { 'size': '1' } }) }}{% if with_seconds %}:{{ form_widget(form.second, { 'attr': { 'size': '1' } }) }}{% endif %}#}
            {#</div>#}
        {#{% endif %}#}
    {#{% endspaceless %}#}
{#{% endblock time_widget %}#}

{#{% block number_widget %}#}
    {#{% spaceless %}#}
        {#{% set type = type|default('text') %}#}
        {#{{ block('field_widget') }}#}
    {#{% endspaceless %}#}
{#{% endblock number_widget %}#}

{#{% block integer_widget %}#}
    {#{% spaceless %}#}
        {#{% set type = type|default('number') %}#}
        {#{{ block('field_widget') }}#}
    {#{% endspaceless %}#}
{#{% endblock integer_widget %}#}

{#{% block money_widget %}#}
    {#{% spaceless %}#}
        {#{{ money_pattern|replace({ '{{ widget }}': block('field_widget') })|raw }}#}
    {#{% endspaceless %}#}
{#{% endblock money_widget %}#}

{#{% block url_widget %}#}
    {#{% spaceless %}#}
        {#{% set type = type|default('url') %}#}
        {#{{ block('field_widget') }}#}
    {#{% endspaceless %}#}
{#{% endblock url_widget %}#}

{#{% block search_widget %}#}
    {#{% spaceless %}#}
        {#{% set type = type|default('search') %}#}
        {#{{ block('field_widget') }}#}
    {#{% endspaceless %}#}
{#{% endblock search_widget %}#}

{#{% block percent_widget %}#}
    {#{% spaceless %}#}
        {#{% set type = type|default('text') %}#}
        {#{{ block('field_widget') }} %#}
    {#{% endspaceless %}#}
{#{% endblock percent_widget %}#}

{#{% block field_widget %}#}
    {#{% spaceless %}#}
        {#{% set type = type|default('text') %}#}
        {#{% if type != 'hidden' %}#}
            {#<div class="controls">#}
        {#{% endif %}#}
        {#{% if prepend is defined %}#}
        {#{% set attr = attr|merge({'class': attr.class|default('') ~ ' span2'}) %}#}
        {#<div class="input-prepend">#}
        {#<span class="add-on">{{ prepend }}</span>#}
        {#{% endif %}#}
        {#<input type="{{ type }}" {{ block('widget_attributes') }}#}
                {#{% if value is not empty %}#}
                    {#value="{{ value }}"#}
                {#{% endif %}#}
                {#{% if errors|length > 0 %}#}
            {#class="error"#}
                {#{% endif %}/>#}
        {#{% if type != 'hidden' %}#}
            {#{% if prepend is defined %}#}
                {#</div>#}
            {#{% endif %}#}
            {#{{ form_errors(form) }}#}
            {#{% if help is defined %}#}
                {#<p class="help-block">{{ help }}</p>#}
            {#{% endif %}#}
            {#</div>#}
        {#{% endif %}#}
    {#{% endspaceless %}#}
{#{% endblock field_widget %}#}

 {#{% block password_widget %}#}
     {#{% spaceless %}#}
         {#{% set type = type|default('password') %}#}
         {#{{ block('field_widget') }}#}
     {#{% endspaceless %}#}
 {#{% endblock password_widget %}#}

{#{% block hidden_widget %}#}
    {#{% set type = type|default('hidden') %}#}
    {#{{ block('field_widget') }}#}
{#{% endblock hidden_widget %}#}

{#{% block email_widget %}#}
    {#{% spaceless %}#}
        {#{% set type = type|default('email') %}#}
        {#{{ block('field_widget') }}#}
    {#{% endspaceless %}#}
{#{% endblock email_widget %}#}

{#&#123;&#35; Labels &#35;&#125;#}

{#{% block generic_label %}#}
    {#{% spaceless %}#}
        {#{% if required %}#}
            {#{% set attr = attr|merge({'class': attr.class|default('') ~ ' control-label required'}) %}#}
        {#{% else %}#}
            {#{% set attr = attr|merge({'class': attr.class|default('') ~ ' control-label'}) %}#}
        {#{% endif %}#}
        {#{% if label is empty %}#}
            {#{% set label = name|humanize %}#}
        {#{% endif %}#}
        {#{% if radio is defined and radio %}#}
            {#{{ label|trans({}, translation_domain) }}#}
        {#{% else %}#}
            {#<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>#}
        {#{% endif %}#}
    {#{% endspaceless %}#}
{#{% endblock %}#}

{#{% block field_label %}#}
    {#{% spaceless %}#}
        {#{% set attr = attr|merge({'for': id}) %}#}
        {#{{ block('generic_label') }}#}
    {#{% endspaceless %}#}
{#{% endblock field_label %}#}

{#{% block form_label %}#}
    {#{% spaceless %}#}
        {#{{ block('generic_label') }}#}
    {#{% endspaceless %}#}
{#{% endblock form_label %}#}

{#&#123;&#35; Rows &#35;&#125;#}

{#{% block repeated_row %}#}
    {#{% spaceless %}#}
        {#{{ block('field_rows') }}#}
    {#{% endspaceless %}#}
{#{% endblock repeated_row %}#}

{#{% block field_row %}#}
    {#{% if errors|length > 0 %}#}
        {#{% set attr = attr|merge({'class': attr.class|default('') ~ ' error control-group'}) %}#}
    {#{% else %}#}
        {#{% set attr = attr|merge({'class': attr.class|default('') ~ ' control-group'}) %}#}
    {#{% endif %}#}
    {#{% spaceless %}#}
        {#<div {% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}" {% endfor %}>#}
        {#{{ form_label(form, label|default(null)) }}#}
        {#{{ form_widget(form) }}#}
        {#</div>#}
    {#{% endspaceless %}#}
{#{% endblock field_row %}#}

{#{% block form_row %}#}
    {#{% spaceless %}#}
        {#<div>#}
            {#&#123;&#35;{{ form_label(form) }}&#35;&#125;#}
            {#{{ form_errors(form) }}#}
            {#{{ form_widget(form) }}#}
        {#</div>#}
    {#{% endspaceless %}#}
{#{% endblock form_row %}#}

{#{% block hidden_row %}#}
    {#{{ form_widget(form) }}#}
{#{% endblock hidden_row %}#}

{#&#123;&#35; Misc &#35;&#125;#}

{#{% block field_enctype %}#}
    {#{% spaceless %}#}
        {#{% if multipart %}enctype="multipart/form-data"{% endif %}#}
    {#{% endspaceless %}#}
{#{% endblock field_enctype %}#}

{#{% block field_errors %}#}
    {#{% spaceless %}#}
        {#{% if errors|length > 0 %}#}
            {#<ul class="error unstyled">#}
                {#{% for error in errors %}#}
                    {#<li><span class="help-inline">{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</span></li>#}
                {#{% endfor %}#}
            {#</ul>#}
        {#{% endif %}#}
    {#{% endspaceless %}#}
{#{% endblock field_errors %}#}

{#{% block field_rest %}#}
    {#{% spaceless %}#}
        {#{% for child in form %}#}
            {#{% if not child.rendered %}#}
                {#{{ form_row(child) }}#}
            {#{% endif %}#}
        {#{% endfor %}#}
    {#{% endspaceless %}#}
{#{% endblock field_rest %}#}

{#&#123;&#35; Support &#35;&#125;#}

{#{% block field_rows %}#}
    {#{% spaceless %}#}
        {#{{ form_errors(form) }}#}
        {#{% for child in form %}#}
            {#{% if errors|length > 0 %}#}
                {#{{ form_row(child, {'attr': {'class': 'error'}}) }}#}
            {#{% else %}#}
                {#{{ form_row(child) }}#}
            {#{% endif %}#}
        {#{% endfor %}#}
    {#{% endspaceless %}#}
{#{% endblock field_rows %}#}

{#{% block widget_attributes %}#}
    {#&#123;&#35;{% set attr = attr|merge({'class': attr.class|default('input-xlarge') ~ ''}) %}&#35;&#125;#}
    {#{% spaceless %}#}
        {#id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}#}
        {#{% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}#}
    {#{% endspaceless %}#}
{#{% endblock widget_attributes %}#}

{#{% block widget_container_attributes %}#}
    {#{% spaceless %}#}
        {#id="{{ id }}"#}
        {#{% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}#}
    {#{% endspaceless %}#}
{#{% endblock widget_container_attributes %}#}